<?php
	class Email extends Model_Abstract{
		
		public function addSentEmail($sender, $sendfrom, $subject, $sendto, $content, $email_type, $priority=0){
			$sql = "insert into emails set sender=?, sendfrom=?, subject=?, sendto=?, content=?,  email_type=?, created=?, created_date=?, priority=?";
			try{
				if($this->_connection->execute($sql,array($sender, $sendfrom, $subject, $sendto, $content, $email_type, time(), date('Y-m-d H:i:s'), $priority))){
					return $this->_connection->getLastInsert();
				}
			}catch (Exception $e){
				QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
				throw new Model_Exception($e->getMessage(), $e->getCode());
			}
		}
		
		public function addShareSentEmail($sender, $sendfrom, $subject, $sendto, $userId, $priority=0){
			$sql = "insert into emails set sender=?, sendfrom=?, subject=?, sendto=?, user_id=?, email_type=?, created=?, created_date=?, priority=?";
			try{
				if($this->_connection->execute($sql,array($sender, $sendfrom, $subject, $sendto, $userId, 'share', time(), date('Y-m-d H:i:s'), $priority)))
					return $this->_connection->getLastInsert();
			}catch (Exception $e){
				QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
				throw new Model_Exception($e->getMessage(), $e->getCode());
			}
		}
		
		public function updateEmailRefer($emailId, $content)
		{
			//$sql = "update emails set content=? where id=? and email_type='share'";
			$sql = "update emails set content=? where id=? ";
			try{
				return $this->_connection->execute($sql,array($content, $emailId));
			}catch (Exception $e){
				QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
				throw new Model_Exception($e->getMessage(), $e->getCode());
			}
		}
		
		public function updateEmailOpenTime($emailId) {
			$sql = "update emails set open_time=?, open_time_date=? where id=?";
			try{
				$now = time();
				return $this->_connection->execute($sql,array($now, date('Y-m-d H:i:s'), $emailId));
			}catch (Exception $e){
				QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
				throw new Model_Exception($e->getMessage(), $e->getCode());
			}
		}
		
		
		public function getEmailOpenTimeDate($emailId)
		{
			$sql = "select open_time_date from emails where id=?";
			try{
				$result =  $this->_connection->query($sql,array($emailId));
				if (count($result) > 0)
					return $result[0]['open_time_date'];
			}catch (Exception $e){
				QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
				throw new Model_Exception($e->getMessage(), $e->getCode());
			}
		}
		
		public function updatePos($emailId, $pos)
		{
			$sql = "SELECT {$pos} FROM `emails` WHERE id=? limit 1";
			try{
				$result =  $this->_connection->query($sql,array($emailId));
				if (count($result)>0 && $result[0][$pos] == 0){
					$sql = "UPDATE `emails` SET {$pos}=? WHERE id=?";
					return $this->_connection->execute($sql,array(time(), $emailId));
				}
			}catch (Exception $e){
				QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
				throw new Model_Exception($e->getMessage(), $e->getCode());
			}
		}
		
		public function getConfirmed($emailId)
		{
			$sql = "SELECT confirmed FROM `emails` where id=?";
			try{
				$result =  $this->_connection->query($sql,array($emailId));
				if (count($result) > 0)
					return $result[0]['confirmed'];
			}catch (Exception $e){
				QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
				throw new Model_Exception($e->getMessage(), $e->getCode());
			}
		}
		public function setConfirmed($emailId)
		{
			$sql = "update emails set confirmed=1 where id=?";
			try{
				$this->_connection->execute($sql,array($emailId));
			}catch (Exception $e){
				QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
				throw new Model_Exception($e->getMessage(), $e->getCode());
			}
		}
	public function updateCompleted($eid)
	{
		$sql = "update emails set completed=?, completed_date=? where id=?";
		try
		{
			$now = time();
			$this->_connection->execute($sql,array($now, date('YmdHis', $now), $eid));
		}
		catch (Exception $e)
		{
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
	
	public function getEmailInfoById($eid)
	{
		$sql = " SELECT * FROM emails WHERE id=? LIMIT 1 ";
		try
		{
			$result = $this->_connection->query($sql, array($eid));
			if(count($result) > 0){
				return $result[0];
			}
		}
		catch (Exception $e)
		{
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}		
	}
}